package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.download.DownloadManager;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportVideoPresenter;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportVideoPresenterCompl;
import com.jwthhealth_pub.old.sportfitness.service.DownloadServie;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 运动健身页面
 * Created by Administrator on 2016/12/30.
 */
public class SportFitnessVideoActivity extends Activity {

    public static final String testUrl = "http://192.168.1.206/api/sport?sdfsd";

    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.download_layout)
    RelativeLayout downloadLayout;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private AssetFileDescriptor afd;
    private ISportVideoPresenter iSportPresenter;
    private SurfaceView mSurfaceView;

    public DownloadManager getDownloadManager() {
        return downloadManager;
    }

    private DownloadManager downloadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_video);
        ButterKnife.bind(this);

        startService(new Intent(this, DownloadServie.class));
        handler.sendEmptyMessageDelayed(1, 50);
        initVideo();
        initTopbar();
    }

    private void initVideo() {
        mSurfaceView = (SurfaceView) findViewById(R.id.head_surfaceview);
        final MediaPlayer mMediaPlayer = new MediaPlayer();

        AssetManager assetManager = this.getAssets();
        afd = null;
        try {
            afd = assetManager.openFd("dian.mp4");
        } catch (IOException e) {
        }
        SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {

                mMediaPlayer.setDisplay(surfaceHolder);
                try {

                    mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mMediaPlayer.setLooping(true);//循环播放
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                } catch (IOException e) {
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        double width = mSurfaceView.getWidth();
        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) mSurfaceView.getLayoutParams();
//获取当前控件的布局对象
        params.height= (int)(width*1.178);//设置当前控件布局的高度
//        mSurfaceView.setLayoutParams(params);//将设置好的布局参数应用到控件中


    }

    private void initTopbar() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick(R.id.btn_download)
    public void onClick() {
        downloadLayout.setVisibility(View.VISIBLE);
        iSportPresenter.download(testUrl, "jwthzip");
    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            iSportPresenter = new ISportVideoPresenterCompl(SportFitnessVideoActivity.this);
            downloadManager = DownloadServie.getDownloadManager();
            if (downloadManager == null) {
                Log.d("SportFitnessVideoActivi", "download is null");
            } else {
                Log.d("SportFitnessVideoActivi", "download isn't null");
            }
        }
    };
}
